/*
**脚本编写:魚離ヤ吥開氺
**脚本作用:仿真随机滑动
**测试人员:魚離ヤ吥開氺
**测试系统:安卓8.1
**测试版本:4.1.1 Alpha2
使用说明:
复制粘贴两个关键函数到自己脚本
sml_move()调用即可
*/
//长距离测试
//sml_move(400, 1800, 800, 230, 1000);
//短距离测试
//sml_move(400, 1000, 400, 300, 1000);

function bezier_curves(cp, t) {
    cx = 3.0 * (cp[1].x - cp[0].x);
    bx = 3.0 * (cp[2].x - cp[1].x) - cx;
    ax = cp[3].x - cp[0].x - cx - bx;
    cy = 3.0 * (cp[1].y - cp[0].y);
    by = 3.0 * (cp[2].y - cp[1].y) - cy;
    ay = cp[3].y - cp[0].y - cy - by;

    tSquared = t * t;
    tCubed = tSquared * t;
    result = {
        "x": 0,
        "y": 0
    }
    result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x;
    result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y;
    return result;
}

//仿真随机带曲线滑动
//qx, qy, zx, zy, time 代表起点x,起点y,终点x,终点y,过程耗时单位毫秒
function sml_move(qx, qy, zx, zy, time) {
    var xxy = [time];
    var point = [];
    var dx0 = {
        "x": qx,
        "y": qy
    }

    var dx1 = {
        "x": random(qx - 100, qx + 100),
        "y": random(qy, qy + 50)
    }
    var dx2 = {
        "x": random(zx - 100, zx + 100),
        "y": random(zy, zy + 50),
    }
    var dx3 = {
        "x": zx,
        "y": zy
    }
    for (var i = 0; i < 4; i++) {

        eval("point.push(dx" + i + ")");

    }
    //log(point[3].x)

    for (let i = 0; i < 1; i += 0.08) {
        xxyy = [parseInt(bezier_curves(point, i).x), parseInt(bezier_curves(point, i).y)]

        xxy.push(xxyy);

    }

    //log(xxy);
    gesture.apply(null, xxy);
}


//sml_move(400, 1800, 800, 230, 2000);

//生成从minNum到maxNum的随机数
function randomNum(minNum, maxNum) {
    switch (arguments.length) {
        case 1:
            return parseInt(Math.random() * minNum + 1, 10);
            break;
        case 2:
            return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
            //或者 Math.floor(Math.random()*( maxNum - minNum + 1 ) + minNum );
            break;
        default:
            return 0;
            break;
    }
}

function up() {
    let rNum = randomNum(100, 400);
    log("up");
    log("x:" + rNum);
    sml_move(rNum, 1000, rNum, rNum, 1000);
}

function down() {
    let rNum = randomNum(100, 400);
    let yNum = randomNum(950, 1000);
    log("down");
    log("x:" + rNum);
    log("y:" + yNum);
    sml_move(rNum, 300, rNum, yNum, 1000);
}

// up();
// down();
let p1 = 1000;

app.launch("com.etoutiao.kanwen");

sleep(p1 * 2);

click("跳过");

sleep(p1 * 5);


console.setPosition(270, 470);
console.show();

let menu = ["资讯", "视频", "榜单", "任务", "我的"];

let module1 = ["推荐", "生活", "娱乐", "科技", "美食", "汽车", "育儿", "情感", "奇闻", "搞笑", "励志", "健康", "时尚", "社会", "旅游", "财经"];

let module2 = ["推荐", "音乐", "搞笑", "娱乐", "影视", "社会", "科技", "体育", "生活", "游戏"];

log(menu);
log(module1);

click(menu[0]);
sleep(p1);
click(module1[0]);

let swl = 5;
let menu_len = menu.length;

let msg_arr = [
    "确实受教了",
    "看了一次可能记不住，我多看几次",
    "很感动，每天看到这么多",
    "我表示吃瓜",
    "有些东西不能强求",
    "经验+3",
    "学习一下",
    "小编的文采不错，我给101分，那1分是你的骄傲",
    "小姐姐很漂亮啊，怎么联系呢",
    "作者：一键三连好吗？用户：下次一定"

];

// 资讯
let zx_func = function () {
    let len = module1.length;

    let b_btn = id("mreturn").findOnce();
    if (null != b_btn) {
        b_btn.click();
    }

    let jl_img = id("main1_timer_image1").findOnce();
    if (null != jl_img) {
        log("奖励");
        jl_img.click();
    }

    let back_btn = id("iv_common_back").findOnce();
    if (null != back_btn) {
        back_btn.click();
    }

    click(menu[0]);
    log(menu[0]);
    sleep(p1);

    let rNum = randomNum(0, len - 1);
    log(rNum);
    let tName = module1[rNum];
    log(tName);

    sleep(p1);

    click(tName, 0);

    sleep(p1);

    let cNum = randomNum(1, 4);
    log("cNum:" + cNum);

    // let cf = click(tName,cNum);

    for (let i = 0; i < cNum; i++) {
        up();
        log("cNum-i:" + i);
        sleep(p1);
    }

    let arr = text(tName).find();
    let aLen = arr.length;
    log("aLen:" + aLen);
    let cLen = aLen - 1;
    log("cLen:" + cLen);

    let cf = click(tName, cLen);

    log(cf);
    if (cf) {
        sleep(p1 * 2);
        let zk = click("展开全文");
        sleep(p1 * 2);
        if (zk) {
            for (let i = 0; i < swl; i++) {
                log("滑动" + i);
                up();
                let hdc = randomNum(3, 10);
                log("阅读:" + hdc + "秒");
                sleep(p1 * hdc);
            }

            let tt = id("tv_common_title").findOnce();
            let title = "";
            if (null != tt) {
                title = tt.getText();
            }
            log(title);

            // 随机分享
            let share_flag = randomNum(0, 1);
            log("share_flag:" + share_flag);
            if (1 == share_flag) {
                let share_btn = id("and_img_share").findOnce();
                log(share_btn);
                if (null != share_btn) {
                    share_btn.click();
                    sleep(p1);
                    let share_type = randomNum(0, 1);
                    log("share_type:" + share_type);
                    if (0 == share_type) {
                        click("微信群");
                        textContains("多选").waitFor();
                        sleep(1000);
                        let quen = click("A磊帝森😉");
                        log(quen);
                        sleep(1000);
                        click("分享");
                        sleep(1000);

                        let bk = textContains("返回").findOnce();
                        log(bk);
                        if (null != bk) {
                            bk.click();
                        }
                    } else {
                        sleep(1000);
                        share_btn.click();
                        click("朋友圈");
                        textContains("发表").waitFor();
                        sleep(2000);
                        click("发表");
                        sleep(2000);
                    }

                }
            }

            sleep(p1 * 3);

            let pl_flag = randomNum(0, 1);
            log("pl_flag:" + pl_flag);
            if (1 == pl_flag) {
                let pl = click("评论得金币");
                log(pl);
                if (pl) {
                    let mpos = randomNum(0, msg_arr.length - 1);
                    let msg = msg_arr[mpos];
                    text("请输入评论内容").setText(msg);
                    sleep(3000);
                    click("发表");
                }
            }

          
            back();
            sleep(p1);
            
        }
    }

}


let view_minute = 5;
// 视频
let sp_func = function () {
    let len = module2.length;
    let b_btn = id("mreturn").findOnce();
    if (null != b_btn) {
        b_btn.click();
    }

    let jl_img = id("main1_timer_image1").findOnce();
    if (null != jl_img) {
        log("奖励");
        jl_img.click();
    }

    let back_btn = id("back").findOnce();
    if (null != back_btn) {
        back_btn.click();
    }

    click(menu[1]);
    log(menu[1]);
    sleep(p1);

    let rNum = randomNum(0, len - 1);
    log(rNum);
    let tName = module2[rNum];
    log(tName);

    sleep(p1);

    click(tName, 0);

    sleep(p1);

    let cNum = randomNum(1, 4);
    log("cNum:" + cNum);

    // let cf = click(tName,cNum);

    for (let i = 0; i < cNum; i++) {
        up();
        log("cNum-i:" + i);
        sleep(p1);
    }

    let arr = text(tName).find();
    let aLen = arr.length;
    log("aLen:" + aLen);
    let cLen = aLen - 1;
    log("cLen:" + cLen);

    let cf = click(tName, cLen);

    log(cf);
    if (cf) {
        sleep(p1 * 2);
        let tt = id("tv_common_title").findOnce();
        let title = "";
        if (null != tt) {
            title = tt.getText();
        }
        log(title);
        sleep(p1 * 3);
        click(257, 293);

        let current = id("current").findOnce();
        log(current);
        let cVal = 0;
        if (null != current) {
            cVal = current.getText();
            log("current:" + cVal);
        }
        let total = id("total").findOnce();
        log(total);
        let tVal = 0;
        if (null != total) {
            tVal = total.getText();
            log("total:" + tVal);
        }

        if (null != current && null != total) {
            let tArr = tVal.split(":");
            log(tArr);
            let cArr = cVal.split(":");
            log(cArr);
            let tv = parseInt(tArr[0]);
            log("tv:" + tv);
            let cv = parseInt(cArr[0]);
            log("cv:" + cv);
            let rv = tv - cv;
            log("rv:" + rv);
            log("view_minute:" + view_minute);
            sleep(p1 * 3);
            if (isNaN(rv)) {
                back();
                sleep(p1);
            }else if (rv >= view_minute) {
                back();
                sleep(p1);
            } else {
                textContains("重播").waitFor();

                let cb = textContains("重播").findOnce();
                log(cb);
                if (null != cb) {
                    // 随机分享
                    let share_flag = randomNum(0, 1);
                    log("share_flag:" + share_flag);
                    if (1 == share_flag) {
                        let share_btn = id("and_img_share").findOnce();
                        log(share_btn);
                        if (null != share_btn) {
                            share_btn.click();
                            sleep(p1);
                            let share_type = randomNum(0, 1);
                            log("share_type:" + share_type);
                            if (0 == share_type) {
                                click("微信群");
                                textContains("多选").waitFor();
                                sleep(1000);
                                let quen = click("A磊帝森😉");
                                log(quen);
                                sleep(1000);
                                click("分享");
                                sleep(1000);

                                let bk = textContains("返回").findOnce();
                                log(bk);
                                if (null != bk) {
                                    bk.click();
                                }
                            } else {
                                sleep(1000);
                                share_btn.click();
                                click("朋友圈");
                                textContains("发表").waitFor();
                                sleep(2000);
                                click("发表");
                                sleep(2000);
                            }

                        }
                    }

                    sleep(p1 * 3);

                    let pl_flag = randomNum(0, 1);
                    log("pl_flag:" + pl_flag);
                    if (1 == pl_flag) {
                        let pl = click("评论得金币");
                        log(pl);
                        if (pl) {
                            let mpos = randomNum(0, msg_arr.length - 1);
                            let msg = msg_arr[mpos];
                            text("请输入评论内容").setText(msg);
                            sleep(3000);
                            click("发表");
                        }
                    }

                    back();

                }

            }

        }


        sleep(p1);
        back();
        sleep(p1);

    }

}





while (true) {
    sleep(p1);
    // zx_func();

    let exec_num = randomNum(0, 1);
    // let exec_num = 1;
    log("exec_num:" + exec_num);
    sleep(p1 * 3);
    if (0 == exec_num) {
        zx_func();
        sleep(p1*3);
        back();
    } else if (1 == exec_num) {
        sp_func();
        sleep(p1*3);
        back();
    }



}


